﻿
// NetClientTestDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "NetClientTest.h"
#include "NetClientTestDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define packet_size (1024*1024)
#define ptest_size 8000

// CNetClientTestDlg 对话框

CNetClientTestDlg::CNetClientTestDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_NETCLIENTTEST_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	ptest = new ptest_state[ptest_size];
}

CNetClientTestDlg::~CNetClientTestDlg()
{
	delete[] ptest;
}

void CNetClientTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO1, m_combo_mode);
}

BEGIN_MESSAGE_MAP(CNetClientTestDlg, CDialogEx)
	ON_MESSAGE(NET_READ_MESSAGE, OnReadMessage)
	//ON_MESSAGE(NET_STAT_MESSAGE, OnStatMessage)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON1, &CNetClientTestDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CNetClientTestDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CNetClientTestDlg::OnBnClickedButton3)
END_MESSAGE_MAP()


// CNetClientTestDlg 消息处理程序

BOOL CNetClientTestDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	ULONGLONG temp[2];
	PList<char> key;
	if (getCmdSeed(key) < 0) MessageBox(TEXT("初始化SEED失败"));
	ULONGLONG t = GetTickCount64();
	key.append((char*)&theApp.m_hInstance, sizeof(HINSTANCE));
	key.append((char*)&this->m_hWnd, sizeof(HWND));
	key.append((char*)&t, sizeof(ULONGLONG));
	key.append((char*)temp, sizeof(temp));
	random.init((const byte*)key.begin(), key.count());

	SetDlgItemText(IDC_IPADDRESS1, TEXT("127.0.0.1"));
	SetDlgItemText(IDC_EDIT1, TEXT("19999"));//_PORT
	SetDlgItemText(IDC_EDIT2, TEXT("100"));//_LINK_NUMBER
	SetDlgItemText(IDC_EDIT3, TEXT("100"));//_PACK_NUMBER
	SetDlgItemText(IDC_EDIT4, TEXT("100"));//_DIS_NUMBER
	SetDlgItemText(IDC_EDIT7, TEXT("0"));//letter

	m_combo_mode.SetCurSel(0);

	SetTimer(1, 100, 0);
	SetTimer(2, 2000, 0);

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CNetClientTestDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CNetClientTestDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CNetClientTestDlg::recvTestPack(ptest_state& pt,const char* pdata, int length)
{
	++pt.n_recv;
	unsigned long long h[2];
	pbox128::hash(h, (const byte *)pdata, length);
	if (pt.n_stat[0] != h[0] || pt.n_stat[1] != h[1]) ++pt.n_error;
	pt.n_ready = 1;
}

void CNetClientTestDlg::sendTestPack(ptest_state & pt)
{
	int len = 0;
	char* data = 0;
	int mode = pt.plink.mode();
	int letter = pt.plink.letter();
	if (mode == _NET_MODE_MESSAGE)
	{
		len = random.range(32, packet_size);
		data = new char[len];
		int n = 0;
		while (n < len)
		{
			int c=random.range(-128, 128);
			if (c != letter) data[n++] = char(c);
		}
	}
	else if (mode == _NET_MODE_WIDE_MESSAGE)
	{
		len = random.range(32, packet_size/2);
		wchar_t* t = new wchar_t[len];
		data = (char*)t;
		int n = 0;
		while (n < len)
		{
			int c = random.range(0, 65536);
			if (c != letter) t[n++] = wchar_t(c);
		}
		len *= 2;
	}
	else if (mode == _NET_MODE_PACKET)
	{
		len = random.range(32, packet_size);
		data = new char[len];
		random.fill(data, len);
	}
	else //if (mode == _NET_MODE_FIXED || mode == _NET_MODE_CELL)
	{
		len = letter;
		data = new char[len];
		random.fill(data, len);
	}
	pt.plink.send(data, len);
	pbox128::hash(pt.n_stat, (const byte*)data, len);
	++pt.n_send;
	--pt.n_plan;
	pt.n_ready = 0;
	delete[] data;
}

void CNetClientTestDlg::OnTimer(UINT_PTR nIDEvent)
{
	if (nIDEvent == 1)
	{
		for (int i = 0; i < ptest_size; ++i)
		{
			if (ptest[i].ready()) sendTestPack(ptest[i]);
		}
	}
	else if (nIDEvent == 2)
	{
		int sum_link = 0, sum_send = 0, sum_recv = 0, sum_error = 0;
		for (int i = 0; i < ptest_size; ++i)
		{
			if (ptest[i].plink.isValid()) ++sum_link;
			sum_send += ptest[i].n_send;
			sum_recv += ptest[i].n_recv;
			sum_error += ptest[i].n_error;
		}
		CString s;
		s.Format(TEXT("当前连接数:%d  总发送包数:%d  总接收包数:%d  错误包数:%d"), sum_link, sum_send, sum_recv, sum_error);
		SetDlgItemText(IDC_EDIT5, s);
	}
	
	CDialogEx::OnTimer(nIDEvent);
}

LRESULT CNetClientTestDlg::OnReadMessage(WPARAM wParam, LPARAM lParam)
{
	PNetItem item;
	while (netQueue.pop(item))
	{
		int index = item.id();
		if (item.code() == _NET_CODE_DATA)//data
		{
			recvTestPack(ptest[index], item.data(), item.length());
		}
		else//stat
		{
			if (item.code() == _NET_CODE_CONNECT)
			{
				ptest[index].plink = item;
			}
			else if (item.code() < 0)
			{
				CString s;
				s.Format(TEXT("ID %d %s"), index, CString(getNetCodeDescribe(item.code())));
				ptest[index].clear();
				SetDlgItemText(IDC_EDIT6, s);
			}
		}
	}
	return 0;
}

void CNetClientTestDlg::OnBnClickedButton1()//连接
{
	CString s;
	int mode = m_combo_mode.GetCurSel()+1;
	GetDlgItemText(IDC_EDIT7, s);
	int letter = _ttoi(s);
	GetDlgItemText(IDC_EDIT1, s);
	int port = _ttoi(s);
	GetDlgItemText(IDC_EDIT2, s);
	int n = _ttoi(s);
	GetDlgItemText(IDC_IPADDRESS1, s);

	for (int i = 0; i < ptest_size; ++i)
	{
		if (!ptest[i].plink.isValid())
		{
			PNetLink::connect(s, port, i, mode, letter);//i 本地连接标识
			ptest[i].n_flag = 1;//启用
			--n;
			if (n == 0) break;
		}
	}
}


void CNetClientTestDlg::OnBnClickedButton2()//启动
{
	CString s;
	GetDlgItemText(IDC_EDIT3, s);//转发包数
	int n = _ttoi(s);
	for (int i = 0; i < ptest_size; ++i)
	{
		if (ptest[i].n_flag)
		{
			ptest[i].n_plan += n;
		}
	}
}


void CNetClientTestDlg::OnBnClickedButton3()//断开
{
	CString s;
	GetDlgItemText(IDC_EDIT4, s);//断开数
	int n = _ttoi(s);

	for (int i = 0; i < ptest_size; ++i)
	{
		if (ptest[i].plink.isValid())
		{
			ptest[i].plink.disconnect();
			--n;
			if (n == 0) break;
		}
	}
}

